package com.fjec.devops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.annotation.OpeLog;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.view.InfoRotationVO;
import com.fjec.common.view.RpcReturnPage;
import com.fjec.devops.feign.BaseClient;
import com.fjec.devops.feign.MarketClient;
import org.apache.commons.collections.MapUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 026.轮播图表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-12-18
 */
@RestController
@RequestMapping("/")
public class DevopsRotationController {

    @Resource
    private BaseClient baseClient;

    @Resource
    private MarketClient marketClient;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 后台查询轮播图列表
     *
     * @param page      分页条件
     * @param markCode  市场编码 市场编码=1则为首页轮播图
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @throws Exception
     */
    @GetMapping("/y/rotation/get/page")
    public JsonResult rpcGetInfoRotationPage(Page page,
                                             @RequestParam(required = false) Integer markCode,
                                             @RequestParam(required = false) String startTime,
                                             @RequestParam(required = false) String endTime) throws Exception {
        RpcReturnPage<InfoRotationVO> result = baseClient.rpcGetInfoRotationPage(page.getCurrent(), page.getSize(), markCode, startTime, endTime);
        List<Map<String, Object>> marketMapList = marketClient.getAllMarket();
        if (result != null && result.getDataList() != null) {
            Long total = result.getTotal();
            List<InfoRotationVO> viewList = result.getDataList();
            for (InfoRotationVO view : viewList) {
                if (view.getLinked() == 0) {
                    view.setLinkUrl("");
                }
                if (view.getMarkCode() == 1) {
                    view.setMarkName("首页");
                } else {
                    for (Map<String, Object> marketMap : marketMapList) {
                        if (view.getMarkCode().intValue() == MapUtils.getIntValue(marketMap, "MarkCode", 0)) {
                            view.setMarkName(MapUtils.getString(marketMap, "MarkName"));
                        }
                    }
                }
            }
            return JsonResult.success()
                    .data(viewList)
                    .total(total)
                    .build();
        } else {
            return JsonResult.failure()
                    .build();
        }
    }

    /**
     * 后台保存或更新轮播图信息
     *
     * @param id       主键Id 为空则为更新
     * @param markCode 市场编码 首页为1
     * @param title    标题
     * @param picId    轮播图Id
     * @param rotationPicUrl   轮播图Url
     * @param linked   是否跳转
     * @param rotationLinkUrl  跳转Url
     * @param sort     排序
     * @param display  是否显示 0=不显示 1=显示
     * @return
     */
    @OpeLog(desc = "保存或更新轮播图信息")
    @PostMapping("/y/rotation/save/or/update")
    public JsonResult rpcSaveOrUpdateInfoRotation(@RequestParam(required = false) Long id,
                                                  @RequestParam Integer markCode,
                                                  @RequestParam String title,
                                                  @RequestParam Long picId,
                                                  @RequestParam String rotationPicUrl,
                                                  @RequestParam Integer linked,
                                                  @RequestParam String rotationLinkUrl,
                                                  @RequestParam Integer sort,
                                                  @RequestParam Integer display) {
        if (markCode == 1) {
            return updateMainPageRotation(id, markCode, title, picId, rotationPicUrl, linked, rotationLinkUrl, sort, display);
        } else {
            return updateMarketRotation(id, markCode, title, picId, rotationPicUrl, linked, rotationLinkUrl, sort, display);
        }
    }

    /**
     * 后台根据Id获取详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/y/rotation/get")
    public JsonResult rpcGetInfoRotationById(@RequestParam Long id) throws Exception {
        InfoRotationVO view = baseClient.rpcGetInfoRotationById(id);
        if (view != null) {
            if (view.getMarkCode() == 1) {
                view.setMarkName("首页");
            } else {
                Map<String, Object> markMap = marketClient.getMarketByMarkCode(view.getMarkCode());
                view.setMarkName(MapUtils.getString(markMap, "markName", ""));
            }
            return JsonResult.success()
                    .data(view)
                    .build();
        } else {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0008.getCode())
                    .message(EnumSysCode.ERR_0008.getMsg())
                    .build();
        }
    }

    /**
     * 后台根据Id删除轮播图
     *
     * @param id
     * @return
     */
    @OpeLog(desc = "根据Id删除轮播图")
    @PostMapping("/y/rotation/del")
    public JsonResult rpcDelInfoRotation(@RequestParam Long id) {
        boolean result = baseClient.rpcDelInfoRotation(id);
        if (result) {
            return JsonResult.success()
                    .code(EnumSysCode.MSG_6004.getCode())
                    .message(EnumSysCode.MSG_6004.getMsg())
                    .build();
        } else {
            return JsonResult.failure()
                    .build();
        }
    }

    /**
     * 根据markCode查询并根据sort排序
     *
     * @param markCode
     * @return
     */
    @GetMapping("/y/rotation/get/by/markCode/sort")
    public JsonResult getInfoRotationByMarkCodeAndSort(@RequestParam Integer markCode) {
        List<Map<String, Object>> mapList = baseClient.rpcGetInfoRotationByMarkCodeAndSort(markCode);
        return JsonResult.success()
                .data(mapList)
                .build();
    }

    /**
     * 后台保存或更新首页轮播图
     *
     * @param id
     * @param markCode
     * @param title
     * @param picId
     * @param picUrl
     * @param linked
     * @param linkUrl
     * @param sort
     * @param display
     * @return
     */
    private JsonResult updateMainPageRotation(Long id, Integer markCode, String title, Long picId, String picUrl,
                                              Integer linked, String linkUrl, Integer sort, Integer display) {
        boolean optRotation = baseClient.rpcSaveOrUpdateInfoRotation(id, markCode, title, picId, picUrl, linked, linkUrl, sort, display);
        if (optRotation) {
            if (id == null || id == 0) {
                // 新增
                return JsonResult.success()
                        .code(EnumSysCode.MSG_6002.getCode())
                        .message(EnumSysCode.MSG_6002.getMsg())
                        .build();
            } else {
                // 更新
                return JsonResult.success()
                        .code(EnumSysCode.MSG_6003.getCode())
                        .message(EnumSysCode.MSG_6003.getMsg())
                        .build();
            }
        } else {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0043.getCode())
                    .message(EnumSysCode.ERR_0043.getMsg())
                    .build();
        }
    }


    /**
     * 后台保存或更新市场轮播图
     *
     * @param id
     * @param markCode
     * @param title
     * @param picId
     * @param picUrl
     * @param linked
     * @param linkUrl
     * @param sort
     * @param display
     * @return
     */
    private JsonResult updateMarketRotation(Long id, Integer markCode, String title, Long picId, String picUrl,
                                            Integer linked, String linkUrl, Integer sort, Integer display) {
        boolean optRotation = baseClient.rpcSaveOrUpdateInfoRotation(id, markCode, title, picId, picUrl, linked, linkUrl, sort, display);
        // 市场轮播图
        List<Map<String, Object>> rotationList = baseClient.rpcGetInfoRotationByMarkCode(markCode);
        boolean optMarket = marketClient.updateMarketRotation(markCode, rotationList);
        if (optRotation && optMarket) {
            // 更新Market缓存
            redisTemplate.delete("fjec:market:marketIndex:" + markCode);
            if (id == null || id == 0) {
                // 新增
                return JsonResult.success()
                        .code(EnumSysCode.MSG_6002.getCode())
                        .message(EnumSysCode.MSG_6002.getMsg())
                        .build();
            } else {
                // 更新
                return JsonResult.success()
                        .code(EnumSysCode.MSG_6003.getCode())
                        .message(EnumSysCode.MSG_6003.getMsg())
                        .build();
            }
        } else {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0043.getCode())
                    .message(EnumSysCode.ERR_0043.getMsg())
                    .build();
        }
    }
}
